package com.kch.system.service.impl;

import java.util.List;

import com.kch.common.core.text.Convert;
import com.kch.common.exception.BusinessException;
import com.kch.system.mapper.SysTagMapper;
import com.kch.system.service.ISysTagService;
import com.kch.system.vo.SysTagVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.kch.system.domain.SysTag;
import com.kch.system.mapper.SysDictDataMapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;


/**
 * 字典 业务层处理
 *
 */
@Service
public class SysTagServiceImpl implements ISysTagService {
    @Autowired
    private SysTagMapper sysTagMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 根据条件分页查询字典类型
     *
     * @param dictType 字典类型信息
     * @return 字典类型集合信息
     */
    @Override
    public List<SysTagVO> selectTagList(SysTag dictType) {
        return sysTagMapper.selectTagList(dictType);
    }

    /*    *//**
     * 根据所有字典类型
     *
     * @return 字典类型集合信息
     *//*
    @Override
    public List<SysTag> selectDictTypeAll()
    {
        return dictTypeMapper.selectDictTypeAll();
    }

    */

    /**
     * 根据字典类型ID查询信息
     *
     * @param dictId 字典类型ID
     * @return 字典类型
     */
    @Override
    public SysTagVO selectTagById(Long dictId) {
        SysTagVO sysTagVO = sysTagMapper.selectTagById(dictId);
        if("1".equals(sysTagVO.getType())){
            sysTagVO.setTypeName("战略性新兴产业集群");
        }else if("2".equals(sysTagVO.getType())){
            sysTagVO.setTypeName("未来产业");
        }
        return sysTagVO;
    }

    /**
     * 根据字典类型查询信息
     *
     * @param dictType 字典类型
     * @return 字典类型
     *//*
    public SysTag selectDictTypeByType(String dictType)
    {
        return dictTypeMapper.selectDictTypeByType(dictType);
    }

    *//**
     * 通过字典ID删除字典信息
     *
     * @param dictId 字典ID
     * @return 结果
     *//*
    @Override
    public int deleteTagIds(Long dictId)
    {
        return dictTypeMapper.deleteDictTypeById(dictId);
    }

    */

    /**
     * 批量删除字典类型
     *
     * @param ids 需要删除的数据
     * @return 结果
     */
    @Override
    public int deleteTagIds(String ids) throws BusinessException {
        Long[] dictIds = Convert.toLongArray(ids);
        return sysTagMapper.deleteTagIds(dictIds);
    }

    /**
     * 新增保存字典类型信息
     *
     * @param dictType 字典类型信息
     * @return 结果
     */
    @Override
    public int insertSysTag(SysTag dictType) {
        return sysTagMapper.insertSysTag(dictType);
    }

    /**
     * 修改保存字典类型信息
     *
     * @param dictType 字典类型信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateTag(SysTag dictType) {
        SysTagVO oldDict = sysTagMapper.selectTagById(dictType.getId());
        if (ObjectUtils.isEmpty(oldDict)) {
            throw new RuntimeException("传入id有误");
        }
        if (!ObjectUtils.isEmpty(sysTagMapper.selectTagByIdNot(dictType.getId(), dictType.getTagName()))) {
            throw new RuntimeException("标签名重复");
        }
        return sysTagMapper.updateTag(dictType);
    }

    @Override
    public List<SysTag> selectByCloumnCode(List<String> list) {
        return sysTagMapper.selectByCloumnCode(list);
    }

    @Override
    public int selectByNameAndType(SysTag tag) {
        return sysTagMapper.selectByNameAndType(tag);
    }


}
